﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

using MyProFile_V2.Model;
using Tickz.Commonz;
using Tickz.IOz;
using VTemplate.Engine;
namespace MyProFile_V2.CreateFile
{
    public class CreateFactory
    {
        /// <summary>
        /// 创建路径 文件夹
        /// </summary>
        /// <param name="hasTab"></param>
        /// <param name="proInfo"></param>
        private static void CreateFactoryPath(Hashtable hasTab, ProInfoModel proInfo)
        {
            //项目路径
            string ProPathStr = proInfo.ProPath;
            //如果不存在就创建目录
            if (!Directory.Exists(ProPathStr))
            {
                Directory.CreateDirectory(ProPathStr);
            }
            if (Directory.Exists(ProPathStr + "\\" + proInfo.ProName + ".Factory"))
            {
                if (!proInfo.IsHasOld)
                {
                    Directory.Delete(ProPathStr + "\\" + proInfo.ProName + ".Factory", true);
                }
                else
                {
                    Directory.Delete(ProPathStr + "\\" + proInfo.ProName + ".Factory\\basez", true);
                }
            }
            Directory.CreateDirectory(ProPathStr + "\\" + proInfo.ProName + ".Factory");
            //开发工具版本
            string toolV = proInfo.ToolVersion;

            //创建Model下的文件夹          
            Directory.CreateDirectory(ProPathStr + "\\" + proInfo.ProName + ".Factory\\bin\\Debug");
            Directory.CreateDirectory(ProPathStr + "\\" + proInfo.ProName + ".Factory\\obj\\Debug");
            Directory.CreateDirectory(ProPathStr + "\\" + proInfo.ProName + ".Factory\\Properties");

            //创建子文件夹
            CreateChildFloder(hasTab, proInfo);
            //创建AssemblyInfo.cs
            string AssemblyInfoStr = Filez.ReadFileByPath(Directory.GetCurrentDirectory() + "\\Template\\SqlServer\\AbstractFactory\\Common\\" + toolV + "\\AssemblyInfo.cs");
            AssemblyInfoStr = AssemblyInfoStr.Replace("[Title]", proInfo.ProName);
            AssemblyInfoStr = AssemblyInfoStr.Replace("[Description]", proInfo.ProAuthor);
            AssemblyInfoStr = AssemblyInfoStr.Replace("[Company]", proInfo.CompanyInfo);
            AssemblyInfoStr = AssemblyInfoStr.Replace("[Product]", proInfo.ProName);
            AssemblyInfoStr = AssemblyInfoStr.Replace("[Copyright]", proInfo.CopyRight);
            AssemblyInfoStr = AssemblyInfoStr.Replace("[guid]", Guid.NewGuid().ToString());
            Filez.WriteFileByPath(AssemblyInfoStr.Replace("\r", "").Replace("\n", "\r\n"), ProPathStr + "\\" + proInfo.ProName + ".Factory\\Properties\\AssemblyInfo.cs");
            //生成Factory层的csproj 文件
            string csprojModel = Filez.ReadFileByPath(Directory.GetCurrentDirectory() + "\\Template\\SqlServer\\AbstractFactory\\Common\\" + toolV + "\\proName.Factory.csproj");
            //替换
            //csproj文件里的Compile
            string compileStr = GetcompileStr(hasTab, proInfo);
            csprojModel = csprojModel.Replace("[proName]", proInfo.ProName);
            csprojModel = csprojModel.Replace("[Compile]", compileStr);
            Filez.WriteFileByPath(csprojModel.Replace("\r", "").Replace("\n", "\r\n"), ProPathStr + "\\" + proInfo.ProName + ".Factory\\" + proInfo.ProName + ".Factory.csproj");

        }
        /// <summary>
        /// 获得csproj文件里的Compile
        /// </summary>
        /// <param name="hasTab"></param>
        /// <param name="proInfo"></param>
        /// <returns></returns>
        private static string GetcompileStr(Hashtable hasTab, ProInfoModel proInfo)
        {
            //csproj文件里的Compile
            string compileStr = "";
            foreach (DataModel item in hasTab.Values)
            {
                //csproj文件里的Compile
                compileStr += "<Compile Include=\"basez\\" + (string.IsNullOrEmpty(item.ParentFloder) ? "" : item.ParentFloder + "\\") + "I" + item.UTableName + ".cs\" />\n";
                compileStr += "<Compile Include=\"" + (string.IsNullOrEmpty(item.ParentFloder) ? "" : item.ParentFloder + "\\") + "I" + item.UTableName + ".cs\" />\n";
            }
            return compileStr;

        }
        /// <summary>
        /// 创建子文件夹夹
        /// </summary>
        /// <param name="hasTab"></param>
        /// <param name="proInfo"></param>
        private static void CreateChildFloder(Hashtable hasTab, ProInfoModel proInfo)
        {
            //项目路径
            string ProPathStr = proInfo.ProPath;
            foreach (DataModel item in hasTab.Values)
            {
                string floderBlow = (proInfo.IsProFloder ? (string.IsNullOrEmpty(item.ParentFloder) ? "" : "\\" + item.ParentFloder + "\\") : "");
                //判断下面的文件夹
                if (!Directory.Exists(ProPathStr + "\\" + proInfo.ProName + ".Factory" + floderBlow))
                {
                    Directory.CreateDirectory(ProPathStr + "\\" + proInfo.ProName + ".Factory" + floderBlow);
                }
                //基本功能
                if (!Directory.Exists(ProPathStr + "\\" + proInfo.ProName + ".Factory\\basez" + floderBlow))
                {
                    Directory.CreateDirectory(ProPathStr + "\\" + proInfo.ProName + ".Factory\\basez" + floderBlow);
                }
                //自定义
                if (!Directory.Exists(ProPathStr + "\\" + proInfo.ProName + ".Factory" + floderBlow))
                {
                    Directory.CreateDirectory(ProPathStr + "\\" + proInfo.ProName + ".Factory" + floderBlow);
                }
            }
        }
        /// <summary>
        /// 创建 工程方法
        /// </summary>
        /// <param name="hasTab"></param>
        /// <param name="proInfo"></param>
        /// <returns></returns>
        public static int CreateMethod(Hashtable hasTab, ProInfoModel proInfo)
        {
            //项目路径
            string ProPathStr = proInfo.ProPath;
            //创建工厂路径
            CreateFactoryPath(hasTab, proInfo);
                        
            //实体模板
            string imodelTmpPath = Directory.GetCurrentDirectory() + "\\Template\\SqlServer\\AbstractFactory\\Factory\\Imodel.ProTmp";
            string def_imodelTmpPath = Directory.GetCurrentDirectory() + "\\Template\\SqlServer\\AbstractFactory\\Factory\\Def_Imodel.ProTmp";
            
           // string dataAccessMoban = TickzTools.ReadFile(Directory.GetCurrentDirectory() + "\\template\\Factory\\FDataAccess.temp");
            
           // string createObjFun = TickzTools.ReadFile(Directory.GetCurrentDirectory() + "\\template\\Factory\\createObjFun.temp");
            foreach (DataModel item in hasTab.Values)
            {
                string floderBlow = (proInfo.IsProFloder ? (string.IsNullOrEmpty(item.ParentFloder) ? "" : "\\" + item.ParentFloder + "\\") : "");
                
                TemplateDocument documentx =
                    new TemplateDocument(imodelTmpPath, Encoding.GetEncoding("utf-8"));
                if (!string.IsNullOrEmpty(item.ParentFloder))
                {
                    documentx.Variables.SetValue("blowFloder", "." + item.ParentFloder);
                }
                documentx.Variables.SetValue("Itemx", item);
                Filez.WriteFileByPathAndEncode(documentx.GetRenderText(), ProPathStr + "\\" + proInfo.ProName + ".Factory\\basez\\" + floderBlow + "I" + item.UTableName + ".cs", proInfo.FileEncode, false);
                if (!proInfo.IsHasOld)
                {
                    TemplateDocument defdocumentx =
                        new TemplateDocument(def_imodelTmpPath, Encoding.GetEncoding("utf-8"));
                    if (!string.IsNullOrEmpty(item.ParentFloder))
                    {
                        defdocumentx.Variables.SetValue("blowFloder", "." + item.ParentFloder);
                    }
                    defdocumentx.Variables.SetValue("Itemx", item);
                    Filez.WriteFileByPathAndEncode(defdocumentx.GetRenderText(), ProPathStr + "\\" + proInfo.ProName + ".Factory\\" + floderBlow + "I" + item.UTableName + ".cs", proInfo.FileEncode, false);
                }
            }
            //写入公用的

            string imodelCommTmpPath = Directory.GetCurrentDirectory() + "\\Template\\SqlServer\\AbstractFactory\\Factory\\ImodelCommon.ProTmp";
            TemplateDocument documentImodelComm = new TemplateDocument(imodelCommTmpPath, Encoding.GetEncoding("utf-8"));
            Filez.WriteFileByPathAndEncode(documentImodelComm.GetRenderText(), ProPathStr + "\\" + proInfo.ProName + ".Factory\\basez\\" + "IModelCommon.cs", proInfo.FileEncode, false);

            string databaseProviderTmpPath = Directory.GetCurrentDirectory() + "\\Template\\SqlServer\\AbstractFactory\\Factory\\DatabaseProvider.ProTmp";
            TemplateDocument documentDatabaseProvider = new TemplateDocument(databaseProviderTmpPath, Encoding.GetEncoding("utf-8"));
            Filez.WriteFileByPathAndEncode(documentDatabaseProvider.GetRenderText(), ProPathStr + "\\" + proInfo.ProName + ".Factory\\basez\\"   + "DatabaseProvider.cs", proInfo.FileEncode, false);
            return 0;
        }
    }
}
